/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.contract.bo.ContractStatisticsBO;
import com.pig4cloud.pigx.contract.entity.ContractOrder;
import com.pig4cloud.pigx.contract.enums.ContractOrderEnum;
import com.pig4cloud.pigx.contract.service.ContractOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 订单表
 *
 * @date 2022-08-11 10:08:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/contractorder")
@Api(value = "contractorder", tags = "订单表管理")
public class ContractOrderController {

	private final ContractOrderService contractOrderService;

	/**
	 * 当前委托分页查询
	 *
	 * @param page          分页对象
	 * @param contractOrder 订单表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('contract_contractorder_view')")
	public R<ContractStatisticsBO<ContractOrder>> getContractOrderPage(Page<ContractOrder> page, ContractOrder contractOrder) {
		QueryWrapper<ContractOrder> wrapper = new QueryWrapper<>();
		wrapper.in("status", ContractOrderEnum.Status.ON_GOING.getValue(),
						ContractOrderEnum.Status.PARTIAL_DEAL.getValue());
		wrapper.eq("is_robot","0");
		if (contractOrder.getCreateTime() != null) {
			String time = contractOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
			wrapper.like("create_time", time);
		}
		if (contractOrder.getId() != null) {
			wrapper.like("id",contractOrder.getId());
		}
		if (contractOrder.getUserId() != null) {
			wrapper.like("user_id",contractOrder.getUserId());
		}
		if (contractOrder.getSide() != null && !contractOrder.getSide().isEmpty()) {
			wrapper.eq("side",contractOrder.getSide());
		}
		if (contractOrder.getSymbolId() != null) {
			wrapper.eq("symbol_id",contractOrder.getSymbolId());
		}
		wrapper.orderByDesc("create_time");
		Page<ContractOrder> orderPage = contractOrderService.page(page, wrapper);
		ContractOrder one = contractOrderService.getBaseMapper().selectOne(
				wrapper.select("sum(num) 'num'", "sum(deal_num) 'dealNum'", "sum(margin) 'margin'")
		);
		ContractStatisticsBO<ContractOrder> bo = new ContractStatisticsBO<>();
		bo.setPage(orderPage);
		bo.setStatistics(one);
		return R.ok(bo);
	}

	/**
	 * 通过id查询订单表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('contract_contractorder_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(contractOrderService.getById(id));
	}

	/**
	 * 新增订单表
	 *
	 * @param contractOrder 订单表
	 * @return R
	 */
	@ApiOperation(value = "新增订单表", notes = "新增订单表")
	@SysLog("新增订单表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('contract_contractorder_add')")
	public R save(@RequestBody ContractOrder contractOrder) {
		return R.ok(contractOrderService.save(contractOrder));
	}

	/**
	 * 修改订单表
	 *
	 * @param contractOrder 订单表
	 * @return R
	 */
	@ApiOperation(value = "修改订单表", notes = "修改订单表")
	@SysLog("修改订单表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('contract_contractorder_edit')")
	public R updateById(@RequestBody ContractOrder contractOrder) {
		return R.ok(contractOrderService.updateById(contractOrder));
	}

	/**
	 * 通过id删除订单表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除订单表", notes = "通过id删除订单表")
	@SysLog("通过id删除订单表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('contract_contractorder_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(contractOrderService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 *
	 * @param contractOrder 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('contract_contractorder_export')")
	public List<ContractOrder> export(ContractOrder contractOrder) {
		return contractOrderService.list(Wrappers.query(contractOrder));
	}

	/**
	 * 获取交易用户数量
	 *
	 * @return
	 */
	@ApiOperation(value = "获取交易用户数量", notes = "获取交易用户数量")
	@GetMapping("/transaction/user")
	@PreAuthorize("@pms.hasPermission('contract_contractorder_view')")
	public R getTransactionUser() {
		return R.ok(contractOrderService.getTransactionUser());
	}
}
